package com.csair.seam.infrastructure.utils;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

public class DateHelper {
	public final static String PATTERN_DATE_TIME="yyyy-MM-dd HH:mm:ss";
	public final static String PATTERN_DATE="yyyy-MM-dd";
	
	/**
	 * @description 获取当前日期年月日时分秒的字符串
	 * @return 'yyyy-MM-dd HH:mm:ss'
	 */
	public static String getCurrentDateTimeStr() {
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PATTERN_DATE_TIME);
		return formatter.format(LocalDateTime.now());
    }
	
	/**
	 * @description 获取当前日期年月日的字符串
	 * @return 'yyyy-MM-dd'
	 */
	public static String getCurrentDateStr() {
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PATTERN_DATE);
		return formatter.format(LocalDateTime.now());
    }
	
	/**
	 * @description 获取指定日期指定格式的字符串
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String formatDateStr(Date date,String pattern) {
		//Date -> LocalDate
		//Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
		
		//Date -> LocalDateTime
		LocalDateTime dateTime = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
		return formatter.format(dateTime);
    }
	
	/**
	 * @description 指定日期date是否在startDate和endDate的日期范围内(注：日期格式yyyy-MM-dd HH:mm:ss)
	 * @param date
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static boolean isFixRange(Date date,Date startDate,Date endDate) {
		LocalDateTime dateTime = date2LocalDateTime(date);
		LocalDateTime startDateTime = date2LocalDateTime(startDate);
		LocalDateTime endDateTime = date2LocalDateTime(endDate);
		return (dateTime.isAfter(startDateTime) && dateTime.isBefore(endDateTime)) || dateTime.isEqual(startDateTime) || dateTime.isEqual(endDateTime);
	}
	
	/**
	 * @description 指定日期date是否在startDate和endDate的日期范围内(字符串类型)
	 * @param date
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static boolean isFixRange(String date,String startDate,String endDate) {
		LocalDate localDate = str2LocalDatetime(date).toLocalDate();
		boolean isAfterOrEqualStartDate = false;
		if(StringUtils.isNotBlank(startDate)) {
			LocalDate startDateTime = str2LocalDatetime(startDate).toLocalDate();
			isAfterOrEqualStartDate = localDate.isAfter(startDateTime) || localDate.isEqual(startDateTime);
		}else {
			isAfterOrEqualStartDate = true;
		}
		boolean isBeforeOrEqualEndDate = false;
		if(StringUtils.isNotBlank(endDate)) {
			LocalDate endDateTime = str2LocalDatetime(endDate).toLocalDate();
			isBeforeOrEqualEndDate = localDate.isBefore(endDateTime) || localDate.isEqual(endDateTime);
		}else {
			isBeforeOrEqualEndDate = true;
		}
		return isAfterOrEqualStartDate && isBeforeOrEqualEndDate;
	}
	
	/**
	 * date转LocalDatetime
	 * @param date
	 * @return
	 */
	private static LocalDateTime date2LocalDateTime(Date date) {
		return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
	}
	
	/**
	 * @description 日期字符串转LocalDatetime
	 * @param dateStr
	 */
	private static LocalDateTime str2LocalDatetime(String dateStr) {
		if(StringUtils.isNotBlank(dateStr) && dateStr.length()==10) {
			dateStr += " 00:00:00";
		}
		return LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern(PATTERN_DATE_TIME));
	}
	
}
